package com.example.campuslink.viewModel

import android.content.Intent
import android.util.Log
import androidx.compose.animation.core.updateTransition
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.campuslink.commen.db.comment.CommentDao
import com.example.campuslink.commen.db.comment.CommentTable
import com.example.campuslink.commen.db.invitation.InvitationTable
import com.example.campuslink.commen.db.message.MessageTable
import com.example.campuslink.commen.db.school.SchoolInfoTable
import com.example.campuslink.commen.db.userInfo.UserInfoTable
import com.example.campuslink.commen.db.userNumber.UserNumberTable
import com.example.campuslink.commen.enums.MessageTypeEnum
import com.example.campuslink.commen.tools.Sp
import com.example.campuslink.commen.tools.SpKey
import com.example.campuslink.commen.tools.putExtraKey
import com.example.campuslink.model.ChildrenCommendData
import com.example.campuslink.model.CommendInfoData
import com.example.campuslink.ui.activity.VideoPlayActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class InvitationDetailViewModel : BaseViewModel() {
    private val _userNumber = MutableLiveData<UserNumberTable>()
    val userNumber = _userNumber
    private val _userInfo = MutableLiveData<UserInfoTable>()
    val userInfo = _userInfo
    private val invitationId = MutableLiveData<String>()
    val invitationIdInfo = invitationId
    private val _invitation = MutableLiveData<InvitationTable>()
    val invitation = _invitation
    fun setInvitation(invitation: InvitationTable) {
        _invitation.value = invitation
    }

    //    private val _choiceImage = MutableLiveData<MutableList<String>>()
//    val choiceImage = _choiceImage
    private val _choiceImage = MutableLiveData<String>()
    val choiceImage = _choiceImage
    private val _editStatus = MutableLiveData<String>("None")
    val editStatus = _editStatus
    private val _schoolUserNumber = MutableLiveData<MutableList<String>>()
    val schoolUserNumberInfo = _schoolUserNumber
    private val _userInfoList = MutableLiveData<MutableList<UserInfoTable>>()
    val userInfoList = _userInfoList
    private val _emojiList = MutableLiveData<String>()
    val emojiList = _emojiList
    private val _callUserNumber = MutableLiveData<String>("")
    val callUserNumber = _callUserNumber
    private val _checkReCall = MutableLiveData<Boolean>()
    val checkReCall = _checkReCall
    private val _commendCount = MutableLiveData<Int>(0)
    val commendCount = _commendCount

    private val _isParentPublic = MutableLiveData<Boolean>(true)
    val isParentPublic = _isParentPublic
    fun setParentPublic(isPublic: Boolean) {
        _isParentPublic.value = isPublic
    }

    private val _attentionStatus = MutableLiveData<String>("关注")
    val attentionStatus = _attentionStatus

    private val _firstCommendList = MutableLiveData<MutableList<CommendInfoData>>()
    val firstCommendList = _firstCommendList


    private val _nowNumberInfo = MutableLiveData<UserInfoTable>()
    val nowNumberInfo = _nowNumberInfo

    fun getNowNumberInfo() {
        viewModelScope.launch(Dispatchers.IO) {
            val userNumber = Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""
            val userInfo = userInfoDao.getUserInfoByUserNumber(userNumber)
            _nowNumberInfo.postValue(userInfo ?: UserInfoTable())
        }
    }

    fun removeLikeCommend(isParent: Boolean, id: Int, number: String, position: Int) {
        viewModelScope.launch(Dispatchers.IO) {
            commentDao?.getAllComment()?.forEach {
                Log.i(
                    "TAG",
                    "addLikeCommend:${id} ${number} ${id == it.id && it.commentUserNumber == number} ${it}"
                )
            }
            val schoolInfo = schoolInfoDao?.getSchoolInfoBySchoolName(
                userNumberDao?.getUserNumber(number)?.schoolName ?: ""
            )
            val commend = commentDao.getCommentById(id, number)
            commend?.loveNumber?.remove(Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: "")
            commentDao.updateComment(commend ?: CommentTable())
            val invitation = invitationDao.getInvitationById(
                Sp.getInt(context, SpKey.INVITATION_ID.key, 0) ?: 0,
                Sp.getString(context, SpKey.INVITATION_NUMBER.key, "") ?: ""
            )

            if (isParent) {
                invitation?.invitationParentCommend?.forEach {
                    if (it.commendTable?.id == id && it.commendTable?.commentUserNumber == number) {
                        it.commendTable = commend
                    }
                }
            } else {
                invitation?.invitationChildrenCommend?.map?.get(position)?.forEach {
                    if (it.commendTable?.id == id && it.commendTable?.commentUserNumber == number) {
                        it.commendTable = commend
                    }
                }
            }
            invitationDao?.updateInvitation(invitation ?: InvitationTable())
            schoolInfo?.mainInvitationList?.forEach {
                if (it.id == invitation?.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }
            schoolInfo?.shoppingInvitationList?.forEach {
                if (it.id == invitation?.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }
            val userNumberInfo = userNumberDao?.getUserNumber(
                Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""
            )

            userNumberInfo?.publishList?.forEach {
                if (it.id == invitation?.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }

            userNumberInfo?.collectList?.forEach {
                if (it.id == invitation?.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }
            userNumberInfo?.browseList?.forEach {
                if (it.id == invitation?.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }
            userNumberDao?.updateUserNumber(userNumberInfo ?: return@launch)

            schoolInfoDao.updateSchoolInfo(schoolInfo ?: SchoolInfoTable())
            getFirstCommend()
            getChildCommend()
        }
    }

    fun addLikeCommend(isParent: Boolean, id: Int, number: String, position: Int) {
        viewModelScope.launch(Dispatchers.IO) {
//            commentDao?.getAllComment()?.forEach {
//                Log.i("TAG", "addLikeCommend: ${it}")
//            }
            val commend = commentDao.getCommentById(id, number)
            val schoolInfo = schoolInfoDao?.getSchoolInfoBySchoolName(
                userNumberDao?.getUserNumber(number)?.schoolName ?: ""
            )
            commend?.loveNumber?.remove(Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: "")
            commend?.loveNumber?.add(Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: "")
            commentDao.updateComment(commend ?: CommentTable())
            val invitation = invitationDao.getInvitationById(
                Sp.getInt(context, SpKey.INVITATION_ID.key, 0) ?: 0,
                Sp.getString(context, SpKey.INVITATION_NUMBER.key, "") ?: ""
            )

            if (Sp.getString(context, SpKey.USER_NUMBER.key, "") != number) {
                val message = MessageTable()
                message.messageType = MessageTypeEnum.BE_LOVE_COMMENT.value
                message.sendNumber = Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""
                message.handNumber = number
                message.commendId = id
                message.messageTime = System.currentTimeMillis()
                message.messageImage = ""
                message.messageContent = "该用户点赞了你的评论"
                messageDao?.insertMessage(message)
                schoolInfo?.messageList?.add(message)
                schoolInfoDao?.updateSchoolInfo(schoolInfo ?: SchoolInfoTable())
            }
            if (isParent) {
                invitation?.invitationParentCommend?.forEach {
                    if (it.commendTable?.id == id && it.commendTable?.commentUserNumber == number) {
                        it.commendTable = commend
                    }
                }
            } else {
                invitation?.invitationChildrenCommend?.map?.get(position)?.forEach {
                    if (it.commendTable?.id == id && it.commendTable?.commentUserNumber == number) {
                        it.commendTable = commend
                    }
                }
            }
            invitationDao?.updateInvitation(invitation ?: InvitationTable())
            schoolInfo?.mainInvitationList?.forEach {
                if (it.id == invitation?.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }

            schoolInfo?.shoppingInvitationList?.forEach {
                if (it.id == invitation?.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }

            val userNumberInfo = userNumberDao?.getUserNumber(
                Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""
            )

            userNumberInfo?.publishList?.forEach {
                if (it.id == invitation?.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }

            userNumberInfo?.collectList?.forEach {
                if (it.id == invitation?.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }
            userNumberInfo?.browseList?.forEach {
                if (it.id == invitation?.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }
            userNumberDao?.updateUserNumber(userNumberInfo ?: return@launch)
            schoolInfoDao.updateSchoolInfo(schoolInfo ?: SchoolInfoTable())
            getFirstCommend()
            getChildCommend()
        }
    }

    fun getAttentionStatus() {
        viewModelScope.launch(Dispatchers.IO) {
            val userId = Sp.getString(context, SpKey.INVITATION_NUMBER.key, "")
            val userNumber = Sp.getString(context, SpKey.USER_NUMBER.key, "")
            val nowUserNumberTableInfo = userNumberDao.getUserNumber(userNumber ?: "")
            val otherUserNumberTableInfo = userNumberDao.getUserNumber(userId ?: "")
            if (nowUserNumberTableInfo != null && otherUserNumberTableInfo != null) {
                if (nowUserNumberTableInfo.followList.contains(userId) && otherUserNumberTableInfo.followList.contains(
                        userNumber
                    )
                ) {
                    _attentionStatus.postValue("朋友")
                } else if (nowUserNumberTableInfo.followList.contains(userId)) {
                    _attentionStatus.postValue("已关注")
                } else {
                    _attentionStatus.postValue("关注")
                }
            }
        }
    }

    fun removeAttenttion() {
        viewModelScope.launch(Dispatchers.IO) {
            val userId = Sp.getString(context, SpKey.INVITATION_NUMBER.key, "")
            val userNumber = Sp.getString(context, SpKey.USER_NUMBER.key, "")
            val nowUserNumberTableInfo = userNumberDao.getUserNumber(userNumber ?: "")
            val otherUserNumberTableInfo = userNumberDao.getUserNumber(userId ?: "")
            nowUserNumberTableInfo?.followList?.removeAll { it == userId }
            otherUserNumberTableInfo?.fansList?.removeAll { it == userNumber }
            userNumberDao.updateUserNumber(nowUserNumberTableInfo ?: return@launch)
            userNumberDao.updateUserNumber(otherUserNumberTableInfo ?: return@launch)
            getAttentionStatus()
        }

    }

    fun addAttention() {
        viewModelScope.launch(Dispatchers.IO) {
            val userId = Sp.getString(context, SpKey.INVITATION_NUMBER.key, "")
            val userNumber = Sp.getString(context, SpKey.USER_NUMBER.key, "")
            val nowUserNumberTableInfo = userNumberDao.getUserNumber(userNumber ?: "")
            val otherUserNumberTableInfo = userNumberDao.getUserNumber(userId ?: "")
            nowUserNumberTableInfo?.followList?.removeAll { it == userId }
            otherUserNumberTableInfo?.fansList?.removeAll { it == userNumber }
            nowUserNumberTableInfo?.followList?.add(userId ?: "")
            otherUserNumberTableInfo?.fansList?.add(userNumber ?: "")
            val schoolInfoTable =
                schoolInfoDao.getSchoolInfoBySchoolName(nowUserNumberTableInfo?.schoolName ?: "")
//            val maxId = messageDao.getMaxId()?:0
            if (schoolInfoTable != null) {
                var messageTable = MessageTable()
//                messageTable.id = maxId + 1
                messageTable.handNumber = userId ?: ""
                messageTable.sendNumber = userNumber ?: ""
                messageTable.messageType = MessageTypeEnum.NEW_ADD_ATTENTION.value
                messageTable.messageTime = System.currentTimeMillis()
                messageTable.messageContent = "对方关注了你"
                messageTable.messageImage = ""
                MessageTable.insertWithId(messageDao, messageTable)
                schoolInfoTable?.messageList?.add(messageTable)
                schoolInfoDao.updateSchoolInfo(schoolInfoTable)
            }
            userNumberDao.updateUserNumber(nowUserNumberTableInfo ?: return@launch)
            userNumberDao.updateUserNumber(otherUserNumberTableInfo ?: return@launch)
            getAttentionStatus()
        }
    }

    fun getFirstCommend() {
        viewModelScope.launch(Dispatchers.IO) {
            var count = 0
            val id = Sp.getInt(context, SpKey.INVITATION_ID.key, -1) ?: -1 // 使用-1作为默认值便于区分
            val number = Sp.getString(context, SpKey.INVITATION_NUMBER.key, "NULL") ?: "NULL"

            val invitation = invitationDao.getInvitationById(id, number)
            _firstCommendList.postValue(invitation?.invitationParentCommend)
            count += invitation?.invitationParentCommend?.size ?: 0
            _commendCount.postValue(count)
        }
    }

    private val _childCommendList = MutableLiveData<Map<Int, MutableList<CommendInfoData>>>()
    val childCommendList = _childCommendList

    fun getChildCommend() {
        viewModelScope.launch(Dispatchers.IO) {
            var count = _commendCount.value ?: 0
            val invitation = invitationDao.getInvitationById(
                Sp.getInt(context, SpKey.INVITATION_ID.key, 0) ?: 0,
                Sp.getString(context, SpKey.INVITATION_NUMBER.key, "") ?: ""
            )


            invitation?.invitationChildrenCommend?.map?.let { map ->
                map.values.forEach { childList ->
                    count += childList.size
                }
                _childCommendList.postValue(map) // 使用postValue更新
            }
            _commendCount.postValue(count)
        }
    }

    fun addChildCommend(content: String, path: String, position: Int, handleNumber: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val mid = Sp.getInt(context, SpKey.INVITATION_ID.key, -1) ?: -1 // 使用-1作为默认值便于区分
            val number = Sp.getString(context, SpKey.INVITATION_NUMBER.key, "NULL") ?: "NULL"
            val invitation = invitationDao.getInvitationById(mid, number)

            if (invitation?.invitationChildrenCommend == null) {
                invitation?.invitationChildrenCommend = ChildrenCommendData()
            }
            val schoolInfo = schoolInfoDao.getSchoolInfoBySchoolName(
                userNumberDao?.getUserNumber(
                    Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""
                )?.schoolName ?: ""
            )
            val userInfo = userInfoDao.getUserInfoByUserNumber(handleNumber)


            val maxId = commentDao.getMaxId() ?: 0
            var commentTable = CommentTable().apply {
                id = maxId + 1
                commentUserNumber = Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""
                if (userInfo == null) {
                    commentContent = content
                } else {
                    commentContent = "回复@ ${userInfo?.userName} : ${content}"
                }
                handUserNumber = handleNumber
                data = System.currentTimeMillis()
                contentImage = path
            }
            commentDao.insertComment(commentTable)
            if (invitation?.invitationChildrenCommend?.map?.containsKey(position) ?: false) {
                val commentTableList = invitation?.invitationChildrenCommend?.map?.get(position)
                val commendInfoData = CommendInfoData()
                commendInfoData.commendTable = commentTable
                commendInfoData.userName = Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""
                commendInfoData.userImg =
                    userInfoDao.getUserInfoByUserNumber(commentTable.commentUserNumber)?.userAvatar
                        ?: ""
                commendInfoData.parentPosition = position
                commentTableList?.add(commendInfoData)
                invitation?.invitationChildrenCommend?.map?.put(
                    position,
                    commentTableList ?: mutableListOf()
                )
            } else {
                val commentTableList = mutableListOf<CommendInfoData>()
                val commendInfoData = CommendInfoData()
                commendInfoData.commendTable = commentTable
                commendInfoData.userName = Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""
                commendInfoData.userImg =
                    userInfoDao.getUserInfoByUserNumber(commentTable.commentUserNumber)?.userAvatar
                        ?: ""
                commendInfoData.parentPosition = position
                commentTableList.add(commendInfoData)
                invitation?.invitationChildrenCommend?.map?.put(position, commentTableList)
            }

            if (Sp.getString(context, SpKey.USER_NUMBER.key, "") != Sp.getString(
                    context,
                    SpKey.INVITATION_NUMBER.key,
                    ""
                ) ?: ""
            ) {
                schoolInfo?.let {
                    var messageTable = MessageTable()
                    messageTable.messageImage = invitation?.invitationImage?.get(0) ?: ""
                    messageTable.messageContent =
                        "用户在帖子中对你进行了评论 ${commentTable.commentContent}"
                    messageTable.messageTime = System.currentTimeMillis()
                    messageTable.messageType = MessageTypeEnum.GET_COMMENT.value
                    messageTable.sendNumber =
                        Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""
                    messageTable.handNumber = handleNumber
                    messageTable.commendId = commentTable.id ?: 0
                    messageDao.insertMessage(messageTable)
                    MessageTable.insertWithId(messageDao, messageTable)
                    schoolInfo.messageList.add(messageTable)
                    if (_callUserNumber?.value != "") {
                        messageTable = MessageTable()
                        messageTable.messageImage = invitation?.invitationImage?.get(0) ?: ""
                        messageTable.messageContent =
                            "用户在帖子中@了你"
                        messageTable.messageTime = System.currentTimeMillis()
                        messageTable.messageType = MessageTypeEnum.CALL_YOUR.value
                        messageTable.sendNumber =
                            Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""
                        messageTable.handNumber = _callUserNumber.value ?: ""
                        messageTable.commendId = commentTable.id ?: 0
                        messageDao.insertMessage(messageTable)
                        MessageTable.insertWithId(messageDao, messageTable)
                        schoolInfo.messageList.add(messageTable)
                    }
                }
                schoolInfoDao.updateSchoolInfo(schoolInfo)
            }
            invitationDao.updateInvitation(invitation ?: return@launch)
            schoolInfo?.mainInvitationList?.forEach {
                if (it.id == invitation.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }

            schoolInfo?.shoppingInvitationList?.forEach {
                if (it.id == invitation?.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }

            val userNumberInfo = userNumberDao?.getUserNumber(
                Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""
            )

            userNumberInfo?.publishList?.forEach {
                if (it.id == invitation.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }

            userNumberInfo?.collectList?.forEach {
                if (it.id == invitation.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }
            userNumberInfo?.browseList?.forEach {
                if (it.id == invitation.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }
            userNumberDao?.updateUserNumber(userNumberInfo ?: return@launch)
            schoolInfoDao.updateSchoolInfo(schoolInfo ?: SchoolInfoTable())
            getChildCommend()
        }
    }

    fun addParentCommend(content: String, path: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val mid = Sp.getInt(context, SpKey.INVITATION_ID.key, -1) ?: -1 // 使用-1作为默认值便于区分
            val number = Sp.getString(context, SpKey.INVITATION_NUMBER.key, "NULL") ?: "NULL"
            val invitation = invitationDao.getInvitationById(mid, number)

            if (invitation?.invitationParentCommend == null) {
                invitation?.invitationParentCommend = mutableListOf()
            }
            val schoolInfo = schoolInfoDao.getSchoolInfoBySchoolName(
                userNumberDao?.getUserNumber(
                    Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""
                )?.schoolName ?: ""
            )

            val maxId = commentDao.getMaxId() ?: 0
            var commentTable = CommentTable().apply {
                id = maxId + 1
                commentUserNumber = Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""
                commentContent = content
                handUserNumber = Sp.getString(context, SpKey.INVITATION_NUMBER.key, "") ?: ""
                data = System.currentTimeMillis()
                contentImage = path
            }

            commentDao.insertComment(commentTable)

            var commendInfoData: CommendInfoData = CommendInfoData()
            commendInfoData.commendTable = commentTable
            commendInfoData.userName =
                userInfoDao.getUserInfoByUserNumber(commentTable.commentUserNumber)?.userName ?: ""
            commendInfoData.userImg =
                userInfoDao.getUserInfoByUserNumber(commentTable.commentUserNumber)?.userAvatar
                    ?: ""
            invitation?.invitationParentCommend?.add(commendInfoData)

            if (Sp.getString(context, SpKey.USER_NUMBER.key, "") != Sp.getString(
                    context,
                    SpKey.INVITATION_NUMBER.key,
                    ""
                ) ?: ""
            ) {
                schoolInfo?.let {
                    var messageTable = MessageTable()
                    messageTable.messageImage = invitation?.invitationImage?.get(0) ?: ""
                    messageTable.messageContent =
                        "用户在帖子中对你进行了评论 ${commentTable.commentContent}"
                    messageTable.messageTime = System.currentTimeMillis()
                    messageTable.messageType = MessageTypeEnum.GET_COMMENT.value
                    messageTable.sendNumber =
                        Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""
                    messageTable.handNumber =
                        Sp.getString(context, SpKey.INVITATION_NUMBER.key, "") ?: ""
                    messageTable.commendId = commentTable.id ?: 0
                    messageDao.insertMessage(messageTable)
                    MessageTable.insertWithId(messageDao, messageTable)
                    schoolInfo.messageList.add(messageTable)
                    if (_callUserNumber?.value != "") {
                        messageTable = MessageTable()
                        messageTable.messageImage = invitation?.invitationImage?.get(0) ?: ""
                        messageTable.messageContent =
                            "用户在帖子中@了你"
                        messageTable.messageTime = System.currentTimeMillis()
                        messageTable.messageType = MessageTypeEnum.CALL_YOUR.value
                        messageTable.sendNumber =
                            Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""
                        messageTable.handNumber = _callUserNumber.value ?: ""
                        messageTable.commendId = commentTable.id ?: 0
                        messageDao.insertMessage(messageTable)
                        MessageTable.insertWithId(messageDao, messageTable)
                        schoolInfo.messageList.add(messageTable)
                    }
                }
                schoolInfoDao.updateSchoolInfo(schoolInfo)
            }
            invitationDao.updateInvitation(invitation ?: return@launch)
            Log.i("TAG", "addParentCommend: ${invitation}")
            schoolInfo?.mainInvitationList?.forEach {
                if (it.id == invitation.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }

            schoolInfo?.shoppingInvitationList?.forEach {
                if (it.id == invitation?.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }

            val userNumberInfo = userNumberDao?.getUserNumber(
                Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""
            )

            userNumberInfo?.publishList?.forEach {
                if (it.id == invitation.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }

            userNumberInfo?.collectList?.forEach {
                if (it.id == invitation.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }
            userNumberInfo?.browseList?.forEach {
                if (it.id == invitation.id && it.userNumber == it.userNumber) {
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }
            userNumberDao?.updateUserNumber(userNumberInfo ?: return@launch)
            schoolInfoDao.updateSchoolInfo(schoolInfo ?: SchoolInfoTable())
            getFirstCommend()
        }
    }


    fun setCallUserNumber(number: String) {
        _callUserNumber.value = number
    }

    fun addEmojiList(emoji: String) {
        _emojiList.postValue(emoji)
    }

    fun getSchoolUserNumber(number: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val userNumber = userNumberDao.getUserNumber(number)
            val schoolInfo =
                schoolInfoDao.getSchoolInfoBySchoolName(userNumber?.schoolName ?: "")
            _schoolUserNumber.postValue(schoolInfo?.schoolUser ?: mutableListOf())
            if (_schoolUserNumber?.value != null) {
                _schoolUserNumber?.value?.forEach {
                    getUserInfoList(it)
                }
            }
        }
    }

    fun getUserInfoList(number: String) {
        val temp = _userInfoList.value ?: mutableListOf()
        viewModelScope.launch(Dispatchers.IO) {
            val userInfo = userInfoDao.getUserInfoByUserNumber(number)
            if (userInfo != null) {
                temp.add(userInfo)
            }
            _userInfoList.postValue(temp)
        }
    }

    fun setEditStatus(status: String) {
        _editStatus.value = status
    }

    fun setImagePath(imagePath: String) {
        _choiceImage.value = imagePath
    }

//    fun addChoiceImage(imagePath: String) {
//        var temp = _choiceImage.value ?: mutableListOf()
//        temp.add(imagePath)
//        _choiceImage.value = temp
//    }


    fun getUserInfo(number: String) {
        viewModelScope.launch(Dispatchers.IO) {
            _userNumber.postValue(userNumberDao?.getUserNumber(number))
            _userInfo.postValue(userInfoDao?.getUserInfoByUserNumber(number))
        }
    }

    fun jumpToVideoPlay(videoPath: String) {
        startActivity(Intent().apply {
            putExtra(putExtraKey.IMAGE_PATH.key, videoPath)
        }, VideoPlayActivity::class.java)
    }
}